package com.example.redisdemo.thread;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author hexinglin
 * @created 2022-09-08 14:53
 **/
public class SemaphoreTest {
    static AtomicInteger A = new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {
        // 表示有2个许可.
       // Semaphore sem = new Semaphore(5);

        for (int i = 0; i < 200; i++) {
            int finalI = i;
            new Thread(() -> {
                try {
                    // 默认使用一个许可.
                  //  sem.acquire();
                    A.incrementAndGet();
                   // System.out.println(Thread.currentThread() + "获取锁");
                   // TimeUnit.SECONDS.sleep(3+ finalI);
                   // System.out.println(Thread.currentThread() + "释放锁");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                  //  sem.release();
                }
            }).start();
        }
        TimeUnit.SECONDS.sleep(3);
        System.out.println(A);
    }
}
